package com.accesshollywood.indexables;

import java.util.Iterator;
import java.util.LinkedHashSet;

import com.accesshollywood.components.EditableListWithPopup.EditableListModel;

public abstract class AbstractIndexedSet<T extends Indexed> extends Indexed {
	protected LinkedHashSet<T>	set	= new LinkedHashSet<T>();

	public AbstractIndexedSet() {
		super();
		initSQL();
	}

	public String toString() {
		String str = "";
		Iterator itr = set.iterator();
		int i = 0;
		while (itr.hasNext()) {
			str += ((Indexed) itr.next()).toString();// .get(i).toString();
			if (set.size() > 1 && i < set.size() - 1) {
				str += "\n";
			}
			i++;
		}
		return str;
	}

	public String toPrint() {
		String str = "        ";
		str += toString();
		return str;
	}

	public boolean isEmpty() {
		return set.isEmpty();
	}

	public Iterator iterator() {
		return set.iterator();
	}

	public int size() {
		return set.size();
	}

	public void clear() {
		set.clear();
	}

	public void add(T newItem) {
		set.add(newItem);
	}

	public void remove(T removeMe) {
		set.remove(removeMe);
	}

	public void remove(int id) {
		remove((T) get(id));
	}

	public T get(int index) {
		Iterator itr = set.iterator();
		int i = 0;
		while (itr.hasNext()) {
			T curr = (T) itr.next();
			if (i++ == index) {
				return curr;
			}
		}
		return null;
	}

	public EditableListModel toEditableListModel() {
		EditableListModel model = new EditableListModel();
		Iterator itr = iterator();
		int i = 0;
		while (itr.hasNext()) {
			Indexed curr = (Indexed) itr.next();
			model.addElement(curr);
		}
		return model;
	}
}
